//
//  ChooseCinemaViewController.m
//  WisdomZhuHai
//
//  Created by Ryan on 14-12-13.
//  Copyright (c) 2014年 wyd. All rights reserved.
//

#import "ChooseCinemaViewController.h"
#import "MovieDetailViewController.h"
#import "HotRateView.h"

//static const CGFloat CHOOSEDATECELL_HEIGHT = 38;
static const CGFloat CINEMALISTCELL_HEIGHT = 100;

@interface ChooseCinemaViewController ()

@property (nonatomic,assign)int currentIndex;
@property (nonatomic,strong)UIView* tapView;

@property (nonatomic,strong)NSMutableArray* cinemaList;

@end

@implementation ChooseCinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.currentIndex = -1;
    
    self.tableView.backgroundColor = [Common colorWithRGB:0xffffff];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    //    [self setEnableRefresh:YES];
    self.pullTableViewDelegate = self;
    self.tableView.tableHeaderView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, self.tableView.bounds.size.width, 0.f)];
    self.title = @"选择影院";
    [self getCinemaListForChoose];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

#pragma mark - Protocol
- (void)getCinemaListForChoose
{
    [[ProtocolManager shareInstance]getStoresList:self.movieId returnCount:0 beginNo:0 area:nil star:nil storeType:@"选择电影院" target:self selector:@selector(getCinemaListForChooseCallback:)];
}

#pragma mark - Callback
- (void)getCinemaListForChooseCallback:(NSDictionary*)dic
{
    NSLog(@"getCinemaListForChooseCallback %@",dic);
    if (dic == nil) {
        return;
    }
    if (self.currentIndex == -1) {
        self.cinemaList = [NSMutableArray array];
    }
    NSArray *cinemaList = [Common valueForKey:P_CINEMAS dic:dic];
    if ([cinemaList isKindOfClass:[NSArray class]]) {
        [self.cinemaList addObjectsFromArray:cinemaList];
    }
    //    if (cinemaList.count < STORE_LIST_RETURN_COUNT) {
    //        [self setEnableLoadMore:NO];
    //    }
    //    else {
    //        [self setEnableLoadMore:YES];
    //    }
    //[self resetPullTableViewState];
    //[self dayTapOn:nil];
    [self.tableView reloadData];
}

#pragma mark - Cell
- (void)loadCinemaListCell:(UITableViewCell*)cell index:(int)index
{
    if (index >= self.cinemaList.count) {
        return;
    }
    NSDictionary* cellData = self.cinemaList[index];
    NSString *name = [Common valueForKey:P_NAME dic:cellData];
    UILabel *lbName = (UILabel*)[cell viewWithTag:1001];
    lbName.text = name;
    [lbName sizeToFit];
    CGRect nameframe = lbName.frame;
    nameframe.size.width = MIN(nameframe.size.width, 270);
    lbName.frame = nameframe;
    
    CGFloat star = [[Common valueForKey:P_RATE dic:cellData] floatValue]/2;
    HotRateView *hotView = [[HotRateView alloc] initWithHotRate:star];
    CGRect frame = hotView.frame;
    frame.origin.x = 45;
    frame.origin.y = (cell.bounds.size.height - hotView.bounds.size.height)/2+2;
    hotView.frame = frame;
    if ([cell viewWithTag:1006] != nil) {
        [[cell viewWithTag:1006] removeFromSuperview];
    }
    UIImageView *moviePic = [[UIImageView alloc] initWithFrame:CGRectMake(lbName.frame.size.width+20,10.0,20,20)];
    moviePic.tag = 1006;
    moviePic.image = [UIImage imageNamed:@"movie/movie_groupon.png"];
    [cell addSubview:moviePic];
    [cell addSubview:hotView];
    
    
    NSString *address = [Common valueForKey:P_ADRESS dic:cellData];
    UILabel *lbAdress = (UILabel*)[cell viewWithTag:1003];
    lbAdress.text = address;
    NSString *price = [Common valueForKey:P_BASE_PRICE dic:cellData];
    UILabel *lbPrice = (UILabel*)[cell viewWithTag:1004];
    lbPrice.text = price;
}

//- (void)loadTitleCell:(UITableViewCell*)cell
//{
//    [[cell viewWithTag:1101] removeFromSuperview];
//    NSArray *date = [self getThreeDays];
//    int i = 0;
//    CGFloat dayViewX = 10;
//    UIView *dateView = [[UIView alloc] initWithFrame:CGRectMake(7.5, 0, 300, CHOOSEDATECELL_HEIGHT)];
//    dateView.tag = 1101;
//    for (NSString *day in date)
//    {
//        NSArray *dayArray = [day componentsSeparatedByString:@"-"];
//        UIView *dayView = [[UIView alloc] initWithFrame:CGRectMake(dayViewX, 0, 85, CHOOSEDATECELL_HEIGHT)];
//        dayView.userInteractionEnabled = YES;
//        UILabel *dayLabel = [[UILabel alloc] initWithFrame:CGRectMake(7, 0, 85, CHOOSEDATECELL_HEIGHT)];
//        dayLabel.font = [UIFont fontWithName:@"TrebuchetMS" size:13.0];
//        dayLabel.tag = 1201;
//        dayLabel.text = [NSString stringWithFormat:@"今天%@月%@日",dayArray[0],dayArray[1]];
//        if (i == 1) {
//            dayLabel.text = [NSString stringWithFormat:@"明天%@月%@日",dayArray[0],dayArray[1]];
//        }
//        else if (i == 2){
//            dayLabel.text = [NSString stringWithFormat:@"后天%@月%@日",dayArray[0],dayArray[1]];
//        }
//        dayViewX = dayViewX + dayView.bounds.size.width + 10;
//        
//        
//        UIView *dateLine = [[UIView alloc] initWithFrame:CGRectMake(2.5, CHOOSEDATECELL_HEIGHT - 2, 90, 3)];
//        dateLine.tag = 1202;
//        if (i == 0) {
//            self.tapView = dayView;
//            dayLabel.textColor = [Common colorWithRGB:0x00bfbf];
//            dateLine.backgroundColor = [Common colorWithRGB:0x4295ed];
//        } else {
//            dayLabel.textColor = [UIColor grayColor];
//            dateLine.backgroundColor = [UIColor clearColor];
//        }
//        [dayView addSubview:dayLabel];
//        [dayView addSubview:dateLine];
//        [dateView addSubview:dayView];
//        UITapGestureRecognizer *dayTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dayTapOn:)];
//        dayTap.cancelsTouchesInView = NO;
//        [dayView addGestureRecognizer:dayTap];
//        i++;
//        [cell.contentView addSubview:dateView];
//    }
//    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(10, CHOOSEDATECELL_HEIGHT, 300, 2)];
//    lineView.backgroundColor = [Common colorWithRGB:0xeaeaea];
//    [cell.contentView addSubview:lineView];
//}

#pragma mark - TableView
//返回table中有多少个组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    // Return the number of sections.
    return 1;
}

//返回table中一个组有多少行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
//    if (section == 0) {
//        return 1;
//    }
    return self.cinemaList.count;
}

//用cellForRowAtIndexPath来得到一个包含每一行显示信息的UITableViewCell对象.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    switch (indexPath.section) {
//        case 0:
//        {
//            
//            cell = [tableView dequeueReusableCellWithIdentifier:@"ChooseDateCell"];
//            // Configure the cell...
//            if(cell == nil)
//            {
//                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ChooseDateCell"];
//            }
//            [self loadTitleCell:cell];
//            cell.selectionStyle = UITableViewCellSelectionStyleNone;
//            break;
//        }
        case 0:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaListCell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[NSBundle mainBundle] loadNibNamed:@"CinemaListCell"  owner:self options:nil][0];
                
            }
            [self loadCinemaListCell:cell index:indexPath.row];
            break;
        }
        default:
        {
            cell = [tableView dequeueReusableCellWithIdentifier:@"Cell"];
            // Configure the cell...
            if(cell == nil)
            {
                cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
            }
        }
    }
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

//每一个cell定义不同的高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
//    if (indexPath.section == 0) {
//        return CHOOSEDATECELL_HEIGHT;
//    }
    if(indexPath.section == 0) {
        return CINEMALISTCELL_HEIGHT;
    }
    else{
        return 100;
    }
}

#pragma mark - Table view delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0){
        if (indexPath.row >= self.cinemaList.count) {
            return;
        }
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
        NSDictionary* dic = self.cinemaList[indexPath.row];
        MovieDetailViewController *movieDetailViewCtrl = [[MovieDetailViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        movieDetailViewCtrl.storeId = [[Common valueForKey:P_ID dic:dic] intValue];
        [self.navigationController pushViewController:movieDetailViewCtrl animated:YES];
        [movieDetailViewCtrl release];
    }
}

//获取今天、明天、后天
//- (NSArray*)getThreeDays
//{
//    NSDate *now = [[NSDate alloc] init];
//    NSTimeInterval secondsPerDay = 24 * 60 * 60;
//    //明天时间
//    NSDate *tomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay];
//    //昨天时间
//    NSDate *afterTomorrow = [[NSDate alloc] initWithTimeIntervalSinceNow:secondsPerDay*2];
//    NSDateFormatter  *dateformatter=[[NSDateFormatter alloc] init];
//    [dateformatter setDateFormat:@"MM-dd"];
//    NSString *todayS = [dateformatter stringFromDate:now];
//    NSString *tomorrowS = [dateformatter stringFromDate:tomorrow];
//    NSString *afterTomorrowS = [dateformatter stringFromDate:afterTomorrow];
//    NSArray *threeDays = [NSArray arrayWithObjects:todayS,tomorrowS,afterTomorrowS, nil];
//    return threeDays;
//}

//- (void)dayTapOn:(UITapGestureRecognizer*)tap
//{
//    NSLog(@"dayTapOn %@",tap);
//    UILabel *font = (UILabel*)[_tapView viewWithTag:1201];
//    UIView *line = (UIView*)[_tapView viewWithTag:1202];
//    font.textColor = [UIColor grayColor];
//    line.backgroundColor = [UIColor clearColor];
//    font = (UILabel*)[tap.view viewWithTag:1201];
//    line = (UIView*)[tap.view viewWithTag:1202];
//    font.textColor = [Common colorWithRGB:0x00bfbf];
//    line.backgroundColor = [Common colorWithRGB:0x4295ed];
//    self.tapView = tap.view;
//
//}

#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    //    self.currentIndex = -1;
    //[self getCinemaList];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    //[self getCinemaList];
}

- (void)resetPullTableViewState
{
    //[self setPullTableIsRefreshing:NO];
    //[self setPullTableIsLoadMore:NO];
}

@end
